package com.ruixun.controller;

import com.ruixun.common.PageData;
import com.ruixun.common.ResponseResult;
import com.ruixun.entity.SysMenu;
import com.ruixun.query.SysMenuPageQuery;
import com.ruixun.rvo.SysMenuRvo;
import com.ruixun.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 菜单管理控制器
 * @author ruixun
 */
@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
@Slf4j
public class SysMenuController {

    private final SysMenuService menuService;

    @ApiOperation(value = "分页查询菜单列表", notes = "支持按菜单名称模糊查询、菜单类型和父级菜单ID查询")
    @PostMapping("/page")
    public ResponseResult<PageData<SysMenuRvo>> getMenuPage(@Valid @RequestBody SysMenuPageQuery query) {
        PageData<SysMenuRvo> pageData = menuService.getMenuPage(query);
        return ResponseResult.success(pageData);
    }



    @ApiOperation(value = "获取当前用户的菜单")
    @GetMapping("/user")
    public ResponseResult<List<SysMenuRvo>> userMenu() {
        List<SysMenuRvo> dataList = menuService.userMenu();
        return ResponseResult.success(dataList);
    }

    @ApiOperation(value = "新增菜单", notes = "新增菜单信息")
    @PostMapping("/add")
    public ResponseResult<Boolean> addMenu(@Valid @RequestBody SysMenu menu) {
        boolean result = menuService.addMenu(menu);
        return result ? ResponseResult.success(true) : ResponseResult.fail("新增菜单失败");
    }

    @ApiOperation(value = "修改菜单", notes = "修改菜单信息")
    @PostMapping("/update")
    public ResponseResult<Boolean> updateMenu(@Valid @RequestBody SysMenu menu) {
        if (menu.getId() == null) {
            return ResponseResult.fail("菜单ID不能为空");
        }
        boolean result = menuService.updateMenu(menu);
        return result ? ResponseResult.success(true) : ResponseResult.fail("修改菜单失败");
    }

    @ApiOperation(value = "删除菜单", notes = "根据ID删除菜单（逻辑删除）")
    @PostMapping("/delete/{id}")
    public ResponseResult<Boolean> deleteMenu(@ApiParam(value = "菜单ID", required = true) @PathVariable Long id) {
        boolean result = menuService.deleteMenu(id);
        return result ? ResponseResult.success(true) : ResponseResult.fail("删除菜单失败");
    }

    @ApiOperation(value = "获取菜单详情", notes = "根据ID获取菜单详细信息")
    @GetMapping("/detail/{id}")
    public ResponseResult<SysMenuRvo> getMenuDetail(@ApiParam(value = "菜单ID", required = true) @PathVariable Long id) {
        SysMenuRvo menu = menuService.getMenuById(id);
        return menu != null ? ResponseResult.success(menu) : ResponseResult.fail("菜单不存在");
    }

    @ApiOperation(value = "获取所有父级菜单", notes = "获取所有一级菜单用于选择父级菜单")
    @GetMapping("/parent/list")
    public ResponseResult<PageData<SysMenuRvo>> getParentMenuList() {
        SysMenuPageQuery query = new SysMenuPageQuery();
        query.setParentId(0L); // 一级菜单的parent_id为0或null
        query.setPageNum(1);
        query.setPageSize(100); // 假设一级菜单不会超过100个
        PageData<SysMenuRvo> pageData = menuService.getMenuPage(query);
        return ResponseResult.success(pageData);
    }

}